<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Agent;
use App\Logic\AgentLogic;
use App\Services\AgentService;
use App\Repositories\AgentRepository;
use App\Http\Requests\Admin\AgentRequest;
use Illuminate\Support\Facades\DB;


class AgentController extends BaseController
{
	
	protected $agentService;
	
	protected $agentRepository;
	
	public function __construct(AgentService $agentService)
	{
		$this->agentService = $agentService;
		
		//$this->agentRepository = $agentRepository  AgentRepository $agentRepository;
	}
    //代理商
	public function index(Request $request){
        $where[] = ['company_type','=',1];
        if ($request->input('categoryId')) {
            $where[] = ['class_id', '=', $request->input('categoryId')];
        }
        //名下财务公司数量筛选
        if ($request->input('customerNum')==1){
            $where[] = ['customer_num','<=','100'];
        }elseif ($request->input('customerNum')==2){
            $where[] = ['customer_num','>','100'];
            $where[] = ['customer_num','<=','500'];
        }elseif ($request->input('customerNum')==3){
            $where[] = ['customer_num','>','500'];
            $where[] = ['customer_num','<=','1000'];
        }elseif ($request->input('customerNum')==4){
            $where[] = ['customer_num','>','1000'];
        }
        //状态
        if ($request->input('companyState')==2){
            $where[]=['company_state','=',0];
        }else if($request->input('companyState')==1){
            $where[]=['company_state','=',1];
        }
        //公司名模糊搜索
        if ($request->input('nameLike')) {
            $where[] = ['company_name', 'like', "%{$request->input('nameLike')}%"];
        }
        //代理商时间筛选
        if ($request->input('companyTime')==1){
            $where[] = [DB::raw(
                    "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(create_date,'%Y-%m-%d'))"
                ),'<=','90'];
        }elseif ($request->input('companyTime')==2){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(create_date,'%Y-%m-%d'))"
            ),'>','90'];
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(create_date,'%Y-%m-%d'))"
            ),'<=','180'];
        }elseif ($request->input('companyTime')==3){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(create_date,'%Y-%m-%d'))"
            ),'>','180'];
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(create_date,'%Y-%m-%d'))"
            ),'<=','365'];
        }elseif ($request->input('companyTime')==4){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(create_date,'%Y-%m-%d'))"
            ),'>','365'];
        }
        //代理商列表
        $agents = AgentLogic::getAgentList($where);

        return $this->view(null,compact('agents'));
    }
    


    /**
     * @param $state
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function state($state,$id)
    {
        $admin = AgentLogic::companyState($id,$state);
//        var_dump($admin);exit();
        if($admin>0)
        {
            flash('更新状态成功')->success()->important();
        }else{
            flash('操作失败')->error()->important();
        }

        return redirect()->route('agent.index');
    }
    
    public function create()
    {   	
    	return view('admin.agent.create');
    }
    
    /**
     * @param AgetnRequest $request
     * @return array
     */
    public function store(Request $request)
    {    	   	
    	$agentLogicModel = new AgentLogic();
    	
    	$company_info = $agentLogicModel->create($request);
    	//dd($company_info);
    	if($company_info['company_name']){   		
    		flash('添加成功')->success()->important();		
    	}else{   		
    		flash('添加失败')->error()->important();
    	}
  	
     	return redirect()->route('agent.index');    
    
     }
     
     /**
      * @param $id
      * @return view
      */
     public function edit($id)
     {
     	$where[] = ['id','=',$id];
     	$agent = AgentLogic::getCompanyDetail($id, $where);
     	return view('admin.agent.edit', compact('agent'));
     }
     
     /**
      * @param AgentRequest $request
      * @param $id
      * @return RedirectResponse
      */
     public function update(Request $request,$id)
     {
     	$agentLogicModel = new AgentLogic();
     	$company_info = $agentLogicModel->update($request,$id);
     	if($company_info){
     	      flash('更新成功')->success()->important();
     	}
     	else{
     	      flash('更新失败')->error()->important();
     	}
     	
     	return redirect()->route('agent.index');
     }

    public function editPassword($id){
        $where[] = ['id','=',$id];
        $finance = AgentLogic::getCompanyDetail($id, $where);
        return view('admin.agent.editPassword', compact('finance'));
    }

    public function password(Request $request,$id)
    {
        $agentLogicModel = new AgentLogic();
        $company_info = $agentLogicModel->password($request,$id);
        if($company_info){
            flash('更新成功')->success()->important();
        }
        else{
            flash('更新失败')->error()->important();
        }

        return redirect()->route('agent.index');
    }
     
     /**
      * @param $id
      * @return RedirectResponse
      */
     public function delete($id)
     {
     	$agentLogicModel = new AgentLogic();
     	$company_info = $agentLogicModel->delete($id);
     	
     	if(empty($company_info))
     	{
     		flash('删除失败')->error()->important();
     		
     		return redirect()->route('agent.index');
     	}
     	
     	
     	flash('删除成功')->success()->important();
     	
     	return redirect()->route('agent.index');
     }
}
